package com.wizinno.android.urineflow.util;

/**
 * @author LiuMei
 * @date 2020-11-25.
 */
public class BytesUtil {
    /**
     * 判断source数组是否以start数组开始
     *
     * @param source
     * @param start
     */
    public static boolean startWith(byte[] source, byte[] start) {
        boolean flag = true;
        if (start.length > 0 & source.length > 0 & source.length >= start.length) {
            for (int i = 0; i < start.length; i++) {
                if (start[i] != source[i]) {
                    flag = false;
                }
            }
        } else {
            flag = false;
        }
        return flag;
    }

    public static byte[] append(byte[] resource, byte newByte) {
        byte[] data = new byte[resource.length + 1];
        for (int i = 0; i < resource.length; i++) {
            data[i] = resource[i];
        }
        data[resource.length] = newByte;
        return data;
    }

    public static byte[] append(byte[] resource, byte[]... bytesArray) {
        int size = resource.length;
        for (int i = 0; i < bytesArray.length; i++) {
            size += bytesArray[i].length;
        }
        byte[] data = new byte[size];
        for (int i = 0; i < resource.length; i++) {
            data[i] = resource[i];
        }
        int index = resource.length;
        for (int i = 0; i < bytesArray.length; i++) {
            for (int j = 0; j < bytesArray[i].length; j++) {
                data[index] = bytesArray[i][j];
                index++;
            }
        }
        return data;
    }
}
